package com.poorstu.manager.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.poorstu.dao.FlowDao;
import com.poorstu.dao.LeaseDao;
import com.poorstu.dao.LoadDao;
import com.poorstu.dao.LoginDao;
import com.poorstu.dao.StudentDao;
import com.poorstu.manager.LoadManager;
import com.poorstu.model.AuditLoadMod;
import com.poorstu.model.AuditsMod;
import com.poorstu.model.FlowLineMod;
import com.poorstu.model.FlowMod;
import com.poorstu.model.LoadMod;
import com.poorstu.model.LoadMoneyMod;
import com.poorstu.model.LoginMod;

public class LoadManagerImpl implements LoadManager{

	LoadDao loadDao;
	StudentDao studentDao;
	LeaseDao leaseDao;
	LoginDao loginDao;
	FlowDao flowDao;
	
	public List<LoadMoneyMod> selectLoadMoneyAll() {
		return loadDao.selectLoadMoneyAll();
	}
	


	public void updateLoad(int loadId, int loadAmount, String loadReason) {
		LoadMod loadMod=studentDao.selectLoadById(loadId);
		LoadMoneyMod loadMoney=leaseDao.selectLoadMoneyById(loadAmount);
		loadMod.setLoadMoney(loadMoney);
		loadMod.setLoadReason(loadReason);
		loadDao.updateLoad(loadMod);
	}

	
	
	public void saveLoad(int allUserId, int flowId, int moneyId,
			String loadReason) {
		FlowMod flow=flowDao.selcetFlowById(flowId);
		LoginMod login=loginDao.selectUserById(allUserId);
		AuditsMod auditMod=new AuditsMod();
		auditMod.setAudStatus(0);
		auditMod.setFlow(flow);
		auditMod.setLogins(login);
		auditMod.setTimes(new Date());
		loadDao.saveAudit(auditMod);
		
		LoadMod loadMod=new LoadMod();
		LoadMoneyMod loadMoney=leaseDao.selectLoadMoneyById(moneyId);
		loadMod.setLoadMoney(loadMoney);
		loadMod.setLoadReason(loadReason);
		loadMod.setLoadTime(new Date());
		loadMod.setAuditss(auditMod);
		loadDao.saveLoad(loadMod);
		
	}

	
	
	


	public List<AuditLoadMod> selectLoadByRoleAndStatus(int roleId,int allUserId) {
		
		List<LoadMod> loadList=loadDao.selectLoadAll();
		
		LoginMod login=loginDao.selectUserById(allUserId);
		int userClassId=login.getClassId().getClassId();
		int userCollegeId=login.getCollege().getCollegeId();
		List<AuditLoadMod> auditLoadList=new ArrayList<AuditLoadMod>();
		for(int i=0;i<loadList.size();i++){
			int status=	loadList.get(i).getAuditss().getAudStatus();
			int classId=loadList.get(i).getAuditss().getLogins().getClassId().getClassId();
			//当用户不属于莫一个班级是 但一定属于某个学院
			int usercollege=loadList.get(i).getAuditss().getLogins().getCollege().getCollegeId();
			
			//int cllegeId=loadList.get(i).getAuditss().getLogins().getClassId().getCollegeId().getCollegeId();
			List<FlowLineMod> flowLineList=loadList.get(i).getAuditss().getFlow().getFlowLineList();
			for(int j=0;j<flowLineList.size();j++)
			{
				if(userClassId==0&&status==flowLineList.get(j).getOrders()&&flowLineList.get(j).getRole().getRoleId()==roleId&&userCollegeId==usercollege){
					AuditLoadMod auditLoad=new AuditLoadMod();
					auditLoad.setName(loadList.get(i).getAuditss().getLogins().getStu().getStuName());
					auditLoad.setStuAccount(loadList.get(i).getAuditss().getLogins().getUserAccount());
					auditLoad.setClasses(loadList.get(i).getAuditss().getLogins().getClassId().getClassName());
					auditLoad.setLoadAmont(loadList.get(i).getLoadMoney().getLoadMoney());
					auditLoad.setAddress(loadList.get(i).getAuditss().getLogins().getStu().getHome().getAddress());
					auditLoad.setApplyDate(loadList.get(i).getLoadTime());
					auditLoad.setLoadId(loadList.get(i).getLoadId());
					auditLoadList.add(auditLoad);
					
				} 
				
				else if(status==flowLineList.get(j).getOrders()&&flowLineList.get(j).getRole().getRoleId()==roleId&&classId==userClassId&&userCollegeId==usercollege)
				{
					AuditLoadMod auditLoad=new AuditLoadMod();
					auditLoad.setName(loadList.get(i).getAuditss().getLogins().getStu().getStuName());
					auditLoad.setStuAccount(loadList.get(i).getAuditss().getLogins().getUserAccount());
					auditLoad.setClasses(loadList.get(i).getAuditss().getLogins().getClassId().getClassName());
					auditLoad.setLoadAmont(loadList.get(i).getLoadMoney().getLoadMoney());
					auditLoad.setAddress(loadList.get(i).getAuditss().getLogins().getStu().getHome().getAddress());
					auditLoad.setApplyDate(loadList.get(i).getLoadTime());
					auditLoad.setLoadId(loadList.get(i).getLoadId());
					auditLoadList.add(auditLoad);
				}
			}
		}
		return auditLoadList;
	}

	
	
	


	public void auditLoad(int audtiStatu, String remark,int loadId) {
	  int auditId=studentDao.selectLoadById(loadId).getAuditss().getAuditId();	
	  AuditsMod auditMod=loadDao.selectAuditById(auditId);
	  int flowId=auditMod.getFlow().getFlowId();
	  int orders=flowDao.selectOder(flowId);
	 int status=auditMod.getAudStatus();
	 
	  if(audtiStatu==0)
	  {
		  audtiStatu=99;
	  }
	  else  if(status==orders){
		  audtiStatu =100;
	  }
	  auditMod.setAudStatus(audtiStatu);
	  auditMod.setRemark(remark);
	  auditMod.setTimes(new Date());
		loadDao.updateAudit(auditMod);
		
		
	}


	
	
	

	public void loadDelete(int loadId) {
		
		LoadMod loadMod=studentDao.selectLoadById(loadId);
		int auditId=loadMod.getAuditss().getAuditId();
		
		loadDao.loadDelete(loadMod);
	}



	public void setLoadDao(LoadDao loadDao) {
		this.loadDao = loadDao;
	}

	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}



	public void setLeaseDao(LeaseDao leaseDao) {
		this.leaseDao = leaseDao;
	}



	public void setLoginDao(LoginDao loginDao) {
		this.loginDao = loginDao;
	}



	public void setFlowDao(FlowDao flowDao) {
		this.flowDao = flowDao;
	}
	
	

}
